# app.py
import time

from flask import Flask, request, jsonify
import pika
import uuid
import json
import threading

app = Flask(__name__)
responses = {}


@app.route('/v1/chat/completions', methods=['POST'])
def chat_completions():
    data = json.loads(request.get_json())
    # 验证请求格式
    # if not data or 'request_num' not in data:
    #     return jsonify({"error": "Invalid request format"}), 400
    print(data)
    time.sleep(10)
    # 模拟处理延迟
    return {
        # "request_num": data.get('request_num'),
        "id": f"chatcmpl-{int(time.time())}",
        "object": "chat.completion",
        "created": int(time.time()),
        "model": data.get('model', 'gpt-3.5-turbo'),
        "choices": [{
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "Hello! How can I help you today?"
            },
            "finish_reason": "stop"
        }],
        "usage": {
            "prompt_tokens": len(str(data)),
            "completion_tokens": 20,
            "total_tokens": len(str(data)) + 20
        }
    }


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)
